package com.dailyblue.java.project.smbms.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dailyblue.java.commons.util.JsonResult;
import com.dailyblue.java.commons.util.ResultTool;
import com.dailyblue.java.project.smbms.user.bean.User;
import com.dailyblue.java.project.smbms.user.handler.GlobalExceptionHandler;
import com.dailyblue.java.project.smbms.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
// 局部跨域：当前controller支持跨域
@CrossOrigin
@Slf4j
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping
    public JsonResult find() {
        return userService.findAllUser();
    }

    @GetMapping("/{page}")
    @SentinelResource(value = "find", fallback = "find", fallbackClass = GlobalExceptionHandler.class)
    public JsonResult find(@PathVariable("page") int page) {
        return userService.findUser(page);
    }

    @GetMapping("/{userName}/{roleId}/{page}")
    public JsonResult find(@PathVariable("userName") String userName, @PathVariable("roleId") Integer roleId, @PathVariable("page") int page) {
        log.info("用户开始搜索，传递过来的，userName:{},roleId:{},page:{}", userName, roleId, page);
        return userService.findUserByUserNameAndRoleId(userName, roleId, page);
    }

    @GetMapping("/id/{id}")
    public JsonResult find(@PathVariable("id") int id, Integer b) {
        return userService.findUserById(id);
    }

    @GetMapping("/{userCode}/{userName}/{userPassword}/{gender}/{birthday}/{phone}/{address}/{userRole}")
    @CrossOrigin
    public JsonResult save(@PathVariable("userCode") String userCode, @PathVariable("userName") String userName, @PathVariable("userPassword") String userPassword, @PathVariable("gender") Integer gender, @PathVariable("birthday") String birthday, @PathVariable("phone") String phone, @PathVariable("address") String address, @PathVariable("userRole") Integer userRole, Integer lastPage) {
        User user = new User(userCode, userName, userPassword, gender, birthday, phone, address, userRole);
        log.info("获取的内容信息是：{}", user);
        userService.saveUser(user, lastPage);
        return ResultTool.success();
    }

    @DeleteMapping("/{id}/{nowPage}/{pages}")
    public JsonResult remove(@PathVariable("id") int id, @PathVariable("nowPage") int nowPage, @PathVariable("pages") int pages) {
        userService.removeUser(id, nowPage, pages);
        return ResultTool.success();
    }

    @PutMapping("/{id}/{userName}/{gender}/{birthday}/{phone}/{address}/{userRole}/{page}")
    public JsonResult update(@PathVariable Integer id, @PathVariable("userName") String userName, @PathVariable("gender") Integer gender, @PathVariable("birthday") String birthday, @PathVariable("phone") String phone, @PathVariable("address") String address, @PathVariable("userRole") Integer userRole, @PathVariable("page") int page) {
        log.info("传递的内容是：{},{},{},{},{},{},{}", id, userName, gender, birthday, phone, address, userRole);
        User user = new User(id, userName, gender, birthday, phone, address, userRole);
        userService.updateUser(user, page);
        return ResultTool.success();
    }

    @PostMapping("/{code}/{pass}")
    public JsonResult login(@PathVariable("code") String code, @PathVariable("pass") String pass) {
        return userService.login(code, pass);
    }
    @GetMapping("/check/{token}")
    public JsonResult findUserIdByToken(@PathVariable("token") String token){
        return userService.findUserIdByToken(token);
    }

    @GetMapping("/id/token")
    public JsonResult findUserByToken(HttpServletRequest request){
        String token = request.getHeader("token");
        JsonResult result = findUserIdByToken(token);
        log.info("根据token获取用户信息是：{}",result);
        if(result.getSuccess()){
            int id = Integer.parseInt(result.getData().toString());
            JsonResult user = userService.findUserById(id);
            return user;
        }
        return ResultTool.fail();
    }

    @GetMapping("/is")
    public JsonResult is(HttpServletRequest request) {
        return userService.is(request.getHeader("token"));
    }
    @GetMapping("/logout")
    public JsonResult logout(HttpServletRequest request) {
        return userService.logout(request.getHeader("token"));
    }
}
